package com.maple.blog.tools;

import com.alibaba.fastjson.JSON;
import com.maple.blog.bean.MessageBean;
import com.maple.blog.config.CustomAttributesConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Jsoup;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.beans.ConstructorProperties;
import java.io.IOException;

/**
 * 描述: com.maple.blog.tools
 *
 * @author hz.lei
 * @date 2018年04月27日 下午11:35
 */
@Component
public class DingTalkTools {
    private static final Logger log = LogManager.getLogger(DingTalkTools.class);
    private final CustomAttributesConfig customAttributesConfig;
    private static final String TYPE_TEXT = "text";

    public void textMessage(MessageBean messageBean) {
        log.info("用户留言：{}", JSON.toJSONString(messageBean));
        if (!StringUtils.isEmpty(this.customAttributesConfig.getDingTalk())) {
            String content = "{\"msgtype\": \"text\", \"text\": {\"content\": \"昵称：" + messageBean.getNickname() + "\n邮箱：" + messageBean.getEmail() + "\n内容：" + messageBean.getContent() + "\"}}";

            try {
                Jsoup.connect(this.customAttributesConfig.getDingTalk()).header("Content-Type", "application/json").requestBody(content).ignoreContentType(true).post();
            } catch (IOException var4) {
                log.error("留言失败! " + messageBean.toString());
            }
        }

    }

    @ConstructorProperties({"customAttributesConfig"})
    public DingTalkTools(final CustomAttributesConfig customAttributesConfig) {
        this.customAttributesConfig = customAttributesConfig;
    }
}
